/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.assembler;

import com.inspur.edp.bff.api.manager.IFSManagerContext;
import com.inspur.edp.bff.api.manager.assembler.IExtendRetrieveAssembler;
import com.inspur.edp.bff.api.manager.context.RetrieveContext;
import com.inspur.edp.bff.spi.action.retrieve.AbstractRetrieveAction;
import com.inspur.edp.bff.spi.action.retrieve.AfterRetrieveAction;
import com.inspur.edp.bff.spi.action.retrieve.BeforeRetrieveAction;
import com.inspur.edp.bff.spi.action.retrieve.DefaultRetrieveAction;

import java.util.ArrayList;

public abstract class AbstractExtendRetrieveAssembler implements IExtendRetrieveAssembler {

    protected abstract ArrayList<BeforeRetrieveAction> getBeforeRetrieveActions(RetrieveContext retrieveContext);

    protected abstract ArrayList<AbstractRetrieveAction> getRetrieveActions(RetrieveContext retrieveContext);

    protected abstract ArrayList<AfterRetrieveAction> getAfterRetrieveActions(RetrieveContext retrieveContext);

    public final void doBeforeRetrieve(IFSManagerContext context, RetrieveContext retrieveContext) {
        ArrayList<BeforeRetrieveAction> actions = getBeforeRetrieveActions(retrieveContext);
        if (actions == null || actions.size() < 1) {
            return;
        }

        for (BeforeRetrieveAction action : actions) {
            action.deal(context);
        }
    }

    public final void doRetrieve(IFSManagerContext context, RetrieveContext retrieveContext) {
        //Dictionary<string, List<IEntityData>> result = new Dictionary<string, List<IEntityData>>();
        ArrayList<AbstractRetrieveAction> actions = getRetrieveActions(retrieveContext);
        if (actions == null || actions.size() < 1) {
            //兼容原有VO没有设置内置Retrieve，但设置了BeforeRetrieve或AfterRetrieve
            actions = new ArrayList<AbstractRetrieveAction>();
            DefaultRetrieveAction detaultAction = new DefaultRetrieveAction(retrieveContext);
            actions.add(detaultAction);
        }

        for (AbstractRetrieveAction action : actions) {
            action.deal(context);
            //MergeQueryData(result, retrieveContext.RetrieveData);
        }

        //return result;
    }

    public final void doAfterRetrieve(IFSManagerContext context, RetrieveContext retrieveContext) {
        ArrayList<AfterRetrieveAction> actions = getAfterRetrieveActions(retrieveContext);
        if (actions == null || actions.size() < 1) {
            return;
        }
        for (AfterRetrieveAction action : actions) {
            action.deal(context);
        }
    }
}
